package com.moli.iov.real.service.impl;

import com.moli.iov.cache.RedisUtil;
import com.moli.iov.contant.RealContant;
import com.moli.iov.real.service.ICacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class CacheServiceImpl implements ICacheService {

    private static final String KEY_LOCATIONSTATE = "IOV:PROTOCOL:LOCATIONSTATE";

    private static final String KEY_DYNAMICSTATE = "IOV:PROTOCOL:DYNAMICSTATE";

    private static final String SPLIT = "_";

    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询全量实时信息json Map
     * @return
     */
    @Override
    public Map<String, String> queryAllRealTimeInfoStringMap() {
        Map<String, String> map = redisUtil.hgetString(RealContant.KEY_MSG_REALTIME);
        return map;
    }

    /**
     * 根据车架号查询车辆实时信息
     * @param vin
     * @return
     */
    @Override
    public String getRealTimeInfoStringByVin(String vin) {
        return redisUtil.hget(RealContant.KEY_MSG_REALTIME, vin);
    }

    /**
     * 根据车架号集合查询实时信息json集合
     * @param vins
     * @return
     */
    @Override
    public List<String> queryRealTimeInfoStringListByVins(List<String> vins) {
        List<String> res = redisUtil.hgetString(RealContant.KEY_MSG_REALTIME, vins);
        if (res != null) {
            res.removeAll(Collections.singleton(null));
        }
        return res;
    }

    /**
     * 获取全量在线车辆MAP
     * @return
     */
    @Override
    public Map<String, String> queryAllOnlineMap() {
        Map<String, String> map = redisUtil.hgetString(RealContant.KEY_ONLINE);
        return map;
    }

    /**
     * 根据车架号集合查询在线车辆集合
     * @param vinList
     * @return
     */
    @Override
    public List<String> queryOnlineList(List<String> vinList) {
        List<String> list = redisUtil.hgetString(RealContant.KEY_ONLINE, vinList);
        if (list != null) {
            list.removeAll(Collections.singleton(null));
        }
        return list;
    }

    /**
     * 根据车架号查询在线状态
     * @param vin
     * @return
     */
    @Override
    public String getOnline(String vin) {
        String tmp = redisUtil.hget(RealContant.KEY_ONLINE, vin);
        return tmp;
    }

    /**
     * 根据车架号集合查询定位状态MAP
     * @param vinList
     * @return
     */
    @Override
    public Map<String, Boolean> getLocationMap(List<String> vinList) {
        List<String> res = redisUtil.hgetString(KEY_LOCATIONSTATE, vinList);
        Map<String, Boolean> map = new HashMap<>();
        if (res != null && !res.isEmpty()) {
            for (String value : res) {
                if (!StringUtils.isEmpty(value) && value.contains(SPLIT)) {
                    String[] arr = value.split(SPLIT);
                    Boolean isLocation = false;
                    if (!StringUtils.isEmpty(arr[1])) {
                        // 0 定位 true，1 未定位 false
                        isLocation = (arr[1].equals("0")) ? true : false;
                    }
                    map.put(arr[0], isLocation);
                }
            }
        }
        return map;
    }

    /**
     * 根据车架号集合查询定位状态MAP
     * @param vinList
     * @return
     */
    @Override
    public Map<String, Integer> getDynamicStateMap(List<String> vinList) {
        List<String> res = null;
        if(vinList==null || vinList.size()<=0){
            Map<String,String> map = redisUtil.hget(KEY_DYNAMICSTATE);
            res = new ArrayList<>();
            res.addAll(map.values());
        }else{
            res = redisUtil.hgetString(KEY_DYNAMICSTATE, vinList);
        }
        Map<String, Integer> result = new HashMap<>();
        if (res != null && !res.isEmpty()) {
            for (String value : res) {
                if (!StringUtils.isEmpty(value) && value.contains(SPLIT)) {
                    String[] arr = value.split(SPLIT);
                    if (!StringUtils.isEmpty(arr[1])) {
                        // 0 锁定 true，1 未锁定 false
                        Integer dynamicstate  = Integer.parseInt(arr[1]) ;
                        result.put(arr[0], dynamicstate);
                    }
                }
            }
        }
        return result;
    }

    @Override
    public List<String> getLocationVinList() {
        Map<String,String> res = redisUtil.hget(KEY_LOCATIONSTATE);
        List<String> list = new ArrayList<>();
        if (res != null && !res.isEmpty()) {
            for (String value : res.values()) {
                if (!StringUtils.isEmpty(value) && value.contains(SPLIT)) {
                    String[] arr = value.split(SPLIT);
                    Boolean isLocation = false;
                    if (!StringUtils.isEmpty(arr[1])) {
                        // 0 定位 true，1 未定位 false
                        isLocation = (arr[1].equals("0"));
                        if (isLocation){
                            list.add(arr[0]);
                        }
                    }
                }
            }
        }
        return list;
    }
}
